<?php

$alphabet = @$_GET['alphabet'];
$listfilter = "";
$listfilter = @$_GET['listfilter'];
$WhereFilter = "";
$listfilter_htmlcode = $PACTS_cfg->remove_duplicated_amps("listfilter=".$listfilter."&amp;");

if ($alphabet==null or $alphabet == ""){
    $alphabet="all";
}


$alphabet_htmlcode = 'alphabet='.$alphabet.'&amp;';

$WhereFilter = $PACTS_cfg->getListHeader($listfilter_htmlcode,$alphabet,"index.php?view=Fmly_az_list","index.php?view=Fmly_allentries","Families",$listfilter,&$WhereFilter);

$link = 'index.php?view=Fmly_az_list&amp;alphabet='.$alphabet.'&amp;';

$WhereFilter = "TRUE ".$WhereFilter;

echo '<div id="main_content">'."\n";


$resultsnumber = 0;
//while ($row = mysql_fetch_array($result)) { 

//	$FamilyId = $row["FamilyId"]; 
//	$shortname = strtoupper(getFormattedFamilyShortName($FamilyId," "));
//	if (strtolower($shortname[0]) == strtolower("$alphabet")) {
//	$resultsnumber = $resultsnumber + 1;
//	}
//}
$sql_all = "SELECT * FROM `".$PACTS_cfg->familytablename."` ORDER BY FamilyShortName";
//echo "<h3>$WhereFilter ".__LINE__." </h3><br />" ;
//echo "<h3>$sql_all ".__LINE__."</h3>" ;

$result = mysql_query($sql_all,$PACTS_cfg->dblink);

//print "<br />SELECT * FROM $family ORDER BY FamilyShortName<br />";

$alternate = "2";
$Print_string = "";
$Print_string_HEADER = "";

if(mysql_num_rows($result)>0){
    $Print_string_HEADER = '<table class="listType1">'."\n";//cellpadding=2 cellspacing=1 border=0>";

    while ($row = mysql_fetch_array($result)) {

        $FamilyId = $row["FamilyId"];
        $shortname = $PACTS_cfg->getFormattedFamilyShortName($FamilyId," ");


        if ((strtolower($shortname[0]) == strtolower("$alphabet")) ||
            (strtoupper($alphabet)=="ALL")) {
            //echo "<h3>$FamilyId = $shortname ".__LINE__."</h3>" ;

            //print "$FamilyId = $shortname<br />";
            //print "***<br />";
            $FilterCount = 0;
            $sqlFilterCount = "SELECT IndividualId FROM ".$PACTS_cfg->individualtablename." Where FamilyId=".$FamilyId." AND ".$WhereFilter;
            $resultIndv = mysql_query($sqlFilterCount ,$PACTS_cfg->dblink);
            $FilterCount = mysql_num_rows($resultIndv );

            //echo "<h3>$FilterCount :: $sqlFilterCount </h3>";
            if($FilterCount > 0) {
                $HeadOfFamily = $PACTS_cfg->getFamilyRoleNameList($FamilyId," + ","Head Of Family");
                $ChildrenCount = 0;
                $OtherCount = 0;
                $sql_c = "SELECT RoleTypeCode, Count(RoleTypeCode) as Count ";
                $sql_c .= "FROM Individual WHERE FamilyId=$FamilyId ";
                $sql_c .= "GROUP BY RoleTypeCode; ";
                $result_c = mysql_query($sql_c ,$PACTS_cfg->dblink);
                while ($row_c = mysql_fetch_array($result_c)) {
                    if($row_c['RoleTypeCode']=="Child") {
                        $ChildrenCount = $row_c['Count'];
                    }
                    if($row_c['RoleTypeCode']=="Other") {
                        $OtherCount = $row_c['Count'];
                    }
                }// end while


                $FamilyDetail = "$shortname <i>($HeadOfFamily";
                if($ChildrenCount>0) {
                    if(strlen($HeadOfFamily)>0) {
                        $FamilyDetail .= " +";
                    }
                    $FamilyDetail .= " ".$ChildrenCount."C";
                }

                if($OtherCount>0) {
                    if(strlen($HeadOfFamily)>0) {
                        $FamilyDetail .=  " +";
                    }
                    $FamilyDetail .= " ".$OtherCount."O";
                }

                $FamilyDetail .= ")</i>";

            } else {
                //no indiviuals infamily
                $FamilyDetail = $shortname." ( empty ) ";
            }
            $resultsnumber = $resultsnumber + 1;
            $Print_string .= $PACTS_cfg->getFmlyNavBar( "", $FamilyId."&amp;".$listfilter_htmlcode.$alphabet_htmlcode, TRUE, TRUE, TRUE, "Fmly_az_list","alphabet=".$alphabet,TRUE,TRUE,$FamilyDetail);
            $Print_string .= "</tr>";
            $FamilyDetail = "";
        }
    }

    $Print_string .='</table>'."\n";
} else {
    $Print_string .= "&nbsp;<br />Sorry - no familes with a surname starting with this letter<p>";
}
// we need to buffer output to be able to show the number of entries at the top of the page
$Print_string_HEADER = '<p id="desc1">Number of entries: '.$resultsnumber.'</p>'."\n".$Print_string_HEADER;
echo $Print_string_HEADER.$Print_string ;

echo '</div>'."\n";

?> 


